package org.csu.storemanage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Param;
import org.csu.storemanage.mapper.CartMapper;
import org.csu.storemanage.mapper.IndexMapper;
import org.csu.storemanage.mapper.ItemMapper;
import org.csu.storemanage.pojo.CartItem;
import org.csu.storemanage.pojo.IndexInfo;
import org.csu.storemanage.pojo.Item;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@MapperScan("org.csu.storemanage.mapper")
public class CartService {
    //地址管理服务
    private CartMapper cartMapper;

    @Autowired
    public CartService(CartMapper cartMapper)//构造注入
    {
        this.cartMapper = cartMapper;
    }

    public List<CartItem> getCartItemListByIOrderBelong(int orderBelong){
        QueryWrapper <CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderBelong",orderBelong);
        List<CartItem> cartItemList = cartMapper.selectList(queryWrapper);
        return cartItemList;
    }

    public void addItem(String itemId,int orderBelong,int quantity){
        UpdateWrapper <CartItem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemId",itemId);
        updateWrapper.eq("orderBelong",orderBelong);
        updateWrapper.setSql("quantity=quantity+" + String.valueOf(quantity));
        cartMapper.update(null,updateWrapper);
    }

    public void subItem(String itemId,int orderBelong,int quantity){
        UpdateWrapper <CartItem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemId",itemId);
        updateWrapper.eq("orderBelong",orderBelong);
        updateWrapper.setSql("quantity=quantity-" + String.valueOf(quantity));
        cartMapper.update(null,updateWrapper);
    }

    public void removeItem(String itemId,int orderBelong){
        QueryWrapper<CartItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemId",itemId);
        queryWrapper.eq("orderBelong",orderBelong);
        cartMapper.delete(queryWrapper);
    }

    @Service
    public static class IndexService {
        @Autowired
        IndexMapper indexMapper;

        public IndexInfo getIndexInfo(){return indexMapper.getIndexInfo();}

        public void updateIndexStoreCard(String newName,String newInfo,String new_description,String oldName){indexMapper.updateIndexStoreCard(newName,newInfo,new_description,oldName);}

        public void updateIndexStoreInformation(String managerInfo,String locationInfo, String phoneInfo, String emailInfo, String validateInfo,String storeName){
            indexMapper.updateIndexStoreInformation(managerInfo,locationInfo,phoneInfo,emailInfo,validateInfo,storeName);
        }
    }

    @Service
    public static class ItemService {
        //商品管理服务
        @Autowired
        private ItemMapper itemMapper;

        public List<Item> getItemListByProductId(@Param("productId") String productId){
            return itemMapper.getItemListByProductId(productId);
        }

        public Item getItem(@Param("itemId") int itemId){
            return itemMapper.getItem(itemId);
        }

        public void addItem(@Param("categoryId")String categoryId, @Param("name")String name, @Param("quantity")int quantity,
                            @Param("unitPrice") BigDecimal unitPrice, @Param("description")String description, @Param("state")String state, @Param("limit")String limit){
            itemMapper.addItem(categoryId, name, quantity, unitPrice, description, state, limit);
        }

        public void editItem(@Param("oldName")String oldName, @Param("quantity")int quantity,
                             @Param("description")String description,@Param("state")String state,@Param("limit")String limit,@Param("newName") String newName,@Param("unitPrice")BigDecimal unitPrice){
            itemMapper.editItem(oldName, quantity, description, state, limit,newName,unitPrice);
        }

        public void deleteItem(@Param("itemId")int itemId){
            itemMapper.deleteItem(itemId);
        }

        public List<Item> getItemList(){
            return itemMapper.getItemList();
        }

        public List<Item> getItemListByCategoryId(@Param("categoryId")String categoryId){
            return itemMapper.getItemListByCategoryId(categoryId);
        }

        public void deleteCategory(@Param("categoryId")String categoryId){itemMapper.deleteCategory(categoryId);}

        public void deleteItemByName(@Param("itemName")String itemName){itemMapper.deleteItemByName(itemName);}
    }
}